/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view.actor;

import model.game.*;
import java.util.ArrayList;
import model.creature.*;
import model.creature.plant.*;
import model.creature.moving_creature.*;
import model.creature.monster.*;
import model.creature.sprite.*;

/**
 *
 * @author Satrio Dewantono
 */
public class Actors {
  private InformationLayer il_;
  private GameEngine game_;
  private ArrayList<NonPlayerActor> staticactorlist;
  private ArrayList<NonPlayerActor> movingactorlist;

  public Actors(GameEngine eng){
    game_ = eng;
    il_ = new InformationLayer(game_);
    staticactorlist = new ArrayList<NonPlayerActor>();
    movingactorlist = new ArrayList<NonPlayerActor>();
    findAllActors();
  }

  public void findAllActors(){
    int map_width = il_.getCurrentMapColumnsCount();
    int map_height = il_.getCurrentMapRowsCount();
    staticactorlist = new ArrayList<NonPlayerActor>();
    movingactorlist = new ArrayList<NonPlayerActor>();
    ArrayList<Plant> plantslist = new ArrayList<Plant>();
    ArrayList<Monster> monsterslist = new ArrayList<Monster>();
    ArrayList<Crystal> crystalslist = new ArrayList<Crystal>();
    ArrayList<DropBox> dropboxeslist = new ArrayList<DropBox>();
    ArrayList<Sprite> spriteslist = new ArrayList<Sprite>();
    plantslist = game_.getCreatures().getPlants().getAllPlants();
    monsterslist = game_.getCreatures().getMonsters().getAllMonsters();
    spriteslist = game_.getCreatures().getSprites().getAllSprites();
    crystalslist = game_.getCreatures().getCrystals().getAllCrystals();
    dropboxeslist = game_.getCreatures().getDropBoxes().getAllDropBoxes();
    for (int i = 0;i< plantslist.size();i++){
      staticactorlist.add(plantslist.get(i).getActor());
    }
    for (int i = 0;i< monsterslist.size();i++){
      movingactorlist.add(monsterslist.get(i).getActor());
    }
    for (int i = 0;i< spriteslist.size();i++){
      if (spriteslist.get(i).isHired()){
	movingactorlist.add(spriteslist.get(i).getActor());
      }
    }
    for (int i = 0;i< crystalslist.size()-2;i++){
      staticactorlist.add(crystalslist.get(i).getActor());
    }
    for (int i = 0;i< dropboxeslist.size()-2;i++){
      staticactorlist.add(dropboxeslist.get(i).getActor());
    }
    setActorsCoordinates();
  }

  public void findMovingActors(){
    int map_width = il_.getCurrentMapColumnsCount();
    int map_height = il_.getCurrentMapRowsCount();
    movingactorlist = new ArrayList<NonPlayerActor>();
    ArrayList<Monster> monsterslist = new ArrayList<Monster>();
    ArrayList<Sprite> spriteslist = new ArrayList<Sprite>();
    monsterslist = game_.getCreatures().getMonsters().getAllMonsters();
    spriteslist = game_.getCreatures().getSprites().getAllSprites();
    for (int i = 0;i< monsterslist.size();i++){
      movingactorlist.add(monsterslist.get(i).getActor());
    }
    for (int i = 0;i< spriteslist.size();i++){
      if (spriteslist.get(i).isHired()){
	movingactorlist.add(spriteslist.get(i).getActor());
      }
    }
    setMovingActorsCoordinates();
  }

  public void tickActors(){
    for (int i = 0;i<movingactorlist.size();i++){
      movingactorlist.get(i).tick();
    }
  }

  public void setActorsCoordinates(){
    for (int i = 0;i<staticactorlist.size();i++){
      setActorCoordinate(staticactorlist.get(i));
    }
    for (int i = 0;i<movingactorlist.size();i++){
      setActorCoordinate(movingactorlist.get(i));
    }
  }

  public void setMovingActorsCoordinates(){
    for (int i = 0;i<movingactorlist.size();i++){
      setActorCoordinate(movingactorlist.get(i));
    }
  }

  public void moveActors(int dx,int dy){
    for (int i = 0;i<staticactorlist.size();i++){
      moveActor(staticactorlist.get(i),dx,dy);
    }
    for (int i = 0;i<movingactorlist.size();i++){
      moveActor(movingactorlist.get(i),dx,dy);
    }
  }

  public void moveActor(NonPlayerActor a,int dx,int dy){
    a.move(dx, dy);
  }

  public void setActorCoordinate(Actor a){
    int pl_x = game_.getCreatures().getPlayer().getCell().getX();
    int pl_y = game_.getCreatures().getPlayer().getCell().getY();

    a.setCoords(175+((a.getAbsX()-pl_x)*25), 145+((a.getAbsY()-pl_y)*25));
  }

  public ArrayList<NonPlayerActor> getStaticActorList(){
    return staticactorlist;
  }

  public ArrayList<NonPlayerActor> getMovingActorList(){
    return movingactorlist;
  }
}
